// auth interceptor
var app = angular.module('app');
app.factory('authInterceptor', ['$rootScope', '$q', '$cookieStore', function ($rootScope, $q, $cookieStore) {
    return {
        request: function (config) {
            // config.headers['x-session-id'] = cookie.get('x-session-id');
            config.headers['book-session-id'] = $cookieStore.get('book-session-id');
            return config;
        },
        requestError: function (rejection) {
            return rejection;
        },
        response: function (res) {
            return res;
        },
        responseError: function (err) {
            if (err.status === 401) {
                if (err.data.errinfo.code === 100023) {
                    // session timeout
                    $rootScope.user = undefined;
                    $rootScope.$emit('loginIntercept', 'sessionTimeout', err);
                } else if (err.data.errinfo.code === 100001) {
                    // not login
                    $rootScope.$emit('loginIntercept', 'notLogin', err);
                }
            }
            
            return $q.reject(err);
        }
    };

}]);